Skip to content

Clean up session ownership keys in Redis#47

Open
ennsharma wants to merge 1 commit into
modelcontextprotocol:mainfrom
ennsharma:fix/session-owner-redis-cleanup
Open

Clean up session ownership keys in Redis#47
ennsharma wants to merge 1 commit into
modelcontextprotocol:mainfrom
ennsharma:fix/session-owner-redis-cleanup

Conversation

@ennsharma

Copy link
Copy Markdown

Fixes #21.

session:${sessionId}:owner keys were written without a TTL and never deleted when the transport was cleaned up, so they accumulated in Redis indefinitely.

Two complementary changes:

  1. Deterministic cleanup: ServerRedisTransport.close() now deletes the session's ownership key. close() runs on every session-termination path — client DELETE (onsessionclosedshutdownSession), SHUTDOWN control messages, and the 5-minute inactivity timeout — so a finished session always removes its key.
  2. Safety-net TTL: setSessionOwner now sets a 30-minute EX on the key, and validateSessionOwnership refreshes it on each successful (authorized) check. Live sessions therefore never expire mid-flight — every request slides the TTL — while keys orphaned by a crashed process (where close() never ran) still get reaped by Redis.

The TTL refresh happens only on successful validation, so an attacker probing someone else's session id can't keep a foreign key alive.

Added unit tests covering: TTL set on write, TTL refresh on authorized validation only, key removal on transport close, and key removal via SHUTDOWN control message. npm run build, npx eslint on the touched files, and the full jest suite (83 tests, 6 suites) all pass.

🤖 Generated with Claude Code

session:${sessionId}:owner keys were written without a TTL and never
deleted, so they accumulated forever (modelcontextprotocol#21).

- Delete the ownership key in ServerRedisTransport.close(), which runs
  on client DELETE, SHUTDOWN control messages, and inactivity timeout
- Set a 30-minute safety-net TTL on the key, refreshed on each
  authorized request, so keys orphaned by a crashed process still
  expire

Fixes modelcontextprotocol#21

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session ownership data is never removed from Redis

1 participant